<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@include file="/admin/do/checklogin.jsp" %>
<%@page import="com.biz.ProductBizImpl"%>
<%@page import="com.entity.Product"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	Product p=new Product();
	int iscover=0;
	List<String> imgs=new ArrayList<String>();
	
	p.setUid(""+((UserInfo)session.getAttribute("loger")).getId());
	
	FileItemFactory fif=new DiskFileItemFactory();
	ServletFileUpload upload=new ServletFileUpload(fif);
	upload.setFileSizeMax(1024*1024*1);
	try{
		List<FileItem> list=upload.parseRequest(request);
		for(FileItem item:list){
			int i=0;
			if(item.isFormField()){
				String val=item.getString("utf-8");
				String name=item.getFieldName();
				if(name.equals("pname"))	p.setPname(val);
				if(name.equals("price"))	p.setPrice(Double.parseDouble(val));
				if(name.equals("stock"))	p.setStock(Integer.parseInt(val));
				if(name.equals("comments"))	p.setComments(val);
				if(name.equals("kind"))		p.setKind(Integer.parseInt(val));
				if(name.equals("iscover"))	iscover=Integer.parseInt(val);
			}else{
				String path=application.getRealPath("images");
				String filename=new File(item.getName()).getName();
				imgs.add("images/"+filename);
				File savefile=new File(path+"/"+filename);
				item.write(savefile);
				i++;
			}
		}
		if(iscover<imgs.size() || iscover>imgs.size()){
			iscover=1;
		}
		if(new ProductBizImpl().addPro(p, imgs, iscover)>0){
%>
		<script>location='../showPro.jsp';</script>
<%
		}else{
%>
		<script>alert("添加失败");location='../addPro.jsp';</script>
<%
		}
	}catch(FileSizeLimitExceededException e){
		e.printStackTrace();
		out.print("<script>alert('图片大小需要小于1MB');history.back();</script>");
	}
%>
